%%% Copyright 2010 Google Inc.
%%%
%%% Licensed under the Apache License, Version 2.0 (the "License");
%%% you may not use this file except in compliance with the License.
%%% You may obtain a copy of the License at
%%%
%%%     http://www.apache.org/licenses/LICENSE-2.0
%%%
%%% Unless required by applicable law or agreed to in writing, software
%%% distributed under the License is distributed on an "AS IS" BASIS,
%%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%%% See the License for the specific language governing permissions and
%%% limitations under the License.

%%% Author <jacoby@google.com> Jacoby Thwaites [http://sites/madcloud]
%%%
%%% Header file for the  process_srv:, process_sup:, process_cache:
%%% and fieldset_srv: modules.

%%% Idle processes, inactive for 20 mins or more, are shutdown
%%% by a cleaner that runs every minute.
-define(CLEANER_INTERVAL_MILLIS, (1 * 60 * 1000)).
-define(MAX_IDLE_MILLIS, (20 * 60 * 1000)).

%%% Timeout on gen_server:call() functions.
-define(CALL_TIMEOUT, infinity).

%%% A process_srv is created by an N op.
-define(PROCESS_STATE, process_state).
-record(?PROCESS_STATE, {compRef = ?UNDEFINED,
                         cookie = ?UNDEFINED,
                         lastUsed = now()}).

%% The input set of an operation has this fixed name.
-define(INPUT_SET_NAME, "input").

%% @type event_fieldset() = #event_fieldset{}.
%% An event fieldset carries field data between the machine and clients.
%% The setName is an output set name in the case of events sent from a client
%% to the machine, and the constant "input" in the case of events sent from
%% the machine to a client.
%% === Fields ===
%% `opRef' = {@type ref()}<p/>
%% `setName' = {@type string()}<p/>
%% `cookie' = {@type term()}<p/>
%% `fields' = {@type list()} of `{ref(), term()}'<p/>
%% `isLast' = {@type boolean()} is normally true. If false, it means
%%            that more fieldsets will follow against this opref.
%% Each fields list relates to an output set defined on the operation.
-define(EVENT_FIELDSET, event_fieldset).
-record(?EVENT_FIELDSET, {
  opRef = ?UNDEFINED   :: ref(),
  setName = ?UNDEFINED :: string(),
  cookie = ?UNDEFINED  :: term(),
  fields = ?UNDEFINED  :: list(),
  isLast = true        :: boolean()
}).
-type(event_fieldset() :: #?EVENT_FIELDSET{}).

%% A fieldset_srv is created by a process_srv handling a collection on an N op,
%% or a fieldset_srv handling a collection on an IRR op.
-define(FIELDSET_STATE, fieldset_state).
-record(?FIELDSET_STATE, {

          % Inherited from parent process.
          compRef = ?UNDEFINED     :: ref(),
          cookie = ?UNDEFINED      :: term(),

          % The emit and collect agendas for this server.
          emitAgenda = []          :: list(),
          collectAgenda = []       :: list(),

          % Consumer ops (OO and IRR) already satisfied by this fieldset.
          satisfied = sets:new()   :: set(),

          % Collected data field values keyed by field ref.
          fieldValues = dict:new() :: dict()
}).

%% Process and Fieldset server handle_call tags.
-define(ACTIVATE, activate).
-define(COLLECT, collect).

%% Server handle_info tags.
-define(LAST_USED, last_used).

%% Process cache module name, ETS table name, record type and key position.
-define(PROCESS_CACHE, process_cache).
-record(?PROCESS_CACHE, {folderRef = ?UNDEFINED,
                         graph = ?UNDEFINED,
                         vertices = ?UNDEFINED,
                         consumers = ?UNDEFINED}).
-define(PROCESS_CACHE_KEY, #?PROCESS_CACHE.folderRef).
